package com.lzy.system.organization.controller;

import com.lzy.conf.JSONResult;
import com.lzy.system.organization.dto.SysDepartmentDTO;
import com.lzy.system.organization.dto.SysDepartmentOrderDTO;
import com.lzy.system.organization.entity.SysDepartment;
import com.lzy.system.organization.service.ISysDepartmentService;
import com.lzy.utils.ValidList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 部门 前端控制器
 * </p>
 *
 * @author lcc
 * @since 2023-11-22
 */
@RestController
@RequestMapping("/organization/sysDepartment")
@Api(tags = "部门管理")
public class SysDepartmentController {
    ISysDepartmentService iSysDepartmentService;

    @Autowired
    public SysDepartmentController(ISysDepartmentService iSysDepartmentService) {
        this.iSysDepartmentService = iSysDepartmentService;
    }

    @ApiOperation("查询部门管理")
    @RequestMapping(value = "getListByPid", method = RequestMethod.POST)
    public JSONResult getListByPid(
            @ApiParam("父id") @RequestParam(value = "pid", required = false,defaultValue = "0") Long pid,
            @ApiParam("公司id")  @RequestParam(value = "companyId", required = false) Integer companyId
    ) throws Exception {
        return JSONResult.ok(iSysDepartmentService.getListByPid(pid,companyId));
    }

    @ApiOperation("添加")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public JSONResult add(
            @ApiParam("部门信息") @RequestBody @Validated() SysDepartmentDTO sysDepartmentDTO
    ) throws Exception {
        return JSONResult.ok(iSysDepartmentService.add(sysDepartmentDTO));
    }
    @ApiOperation("修改")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public JSONResult update(
            @ApiParam("部门信息") @RequestBody @Validated() SysDepartmentDTO sysDepartmentDTO
    ) throws Exception {
        return JSONResult.ok(iSysDepartmentService.update(sysDepartmentDTO));
    }

    @ApiOperation("批量删除部门(会递归删除)")
    @RequestMapping(value = "del", method = RequestMethod.DELETE)
    public JSONResult del(
            @ApiParam("部门列表") @RequestBody @Validated() ValidList<SysDepartment> sysDepartments
    ) throws Exception {
        return JSONResult.ok(iSysDepartmentService.del(sysDepartments));
    }

    @ApiOperation("保存部门排序")
    @RequestMapping(value = "saveOrder", method = RequestMethod.POST)
    public JSONResult saveOrder(
            @ApiParam("部门列表") @RequestBody @Validated() ValidList<SysDepartmentOrderDTO> sysDepartmentOrderDTOS
    ) throws Exception {
        iSysDepartmentService.saveOrder(sysDepartmentOrderDTOS);
        return JSONResult.ok();
    }

    @ApiOperation("根据id查部门")
    @RequestMapping(value = "/getById/{id}", method = RequestMethod.GET)
    public JSONResult getById(@ApiParam("部门id") @PathVariable(value = "id",required = true) long id) throws Exception {
        return JSONResult.ok(iSysDepartmentService.getById(id));
    }
}
